package com.hskn.hss.module.tlfactypeitem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlfacilitietype.entity.TlFacilitiesType;
import com.hskn.hss.module.tlfactypeitem.entity.TlFacilitiesTypeItems;
import com.hskn.hss.module.tlfactypeitem.mapper.TlFacilitiesTypeItemsMapper;
import com.hskn.hss.module.tlfactypeitem.service.ITlFacilitiesTypeItemsService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-12
 */
@Service
public class TlFacilitiesTypeItemsServiceImpl extends ServiceImpl<TlFacilitiesTypeItemsMapper, TlFacilitiesTypeItems> implements ITlFacilitiesTypeItemsService {

    @Resource
    TlFacilitiesTypeItemsMapper tlFacilitiesTypeItemsMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlFacilitiesTypeItems tlFacilitiesTypeItems = params.getObject("data", TlFacilitiesTypeItems.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlFacilitiesTypeItems.setCreateby(uid);
        tlFacilitiesTypeItems.setCreatetime(new Date());
        tlFacilitiesTypeItems.setDeptid(deptid);
        int i = tlFacilitiesTypeItemsMapper.insert(tlFacilitiesTypeItems);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlFacilitiesTypeItems tlFacilitiesTypeItems = params.getObject("data", TlFacilitiesTypeItems.class);
        tlFacilitiesTypeItems.setUpdateby(uid);
        tlFacilitiesTypeItems.setUpdatetime(new Date());
        int i = tlFacilitiesTypeItemsMapper.updateById(tlFacilitiesTypeItems);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlFacilitiesTypeItemsMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }
    @Override
    public AjaxResult deletebatchbyid(TlFacilitiesTypeItems tlFacilitiesTypeItems) throws HssException {
        String ids = tlFacilitiesTypeItems.getIds();
        String[] idArray = ids.split(",");
        tlFacilitiesTypeItemsMapper.deleteBatchIds(Arrays.asList(idArray));
        return AjaxResult.success("操作成功");
    }
    @Override
    public TlFacilitiesTypeItems getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlFacilitiesTypeItems tlFacilitiesTypeItems = tlFacilitiesTypeItemsMapper.selectById(id);
        return tlFacilitiesTypeItems;
    }

    @Override
    public List getSelectList(JSONObject params) {
        Map<String, Object> map = new HashMap();
        map.put("facilitietypeid", params.getString("facilitietypeid"));
        List resultList = tlFacilitiesTypeItemsMapper.getSelectList(map);
        return resultList;
    }

    @Override
    @DataScope(deptAlias = "deptid", userAlias = "createby, updateby")
    public JSONObject getList(BaseEntity entity, JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("itemtypename", params.getString("itemtypename"));
        map.put("facilitietypeid", params.getString("facilitietypeid"));
        map.put("params", entity.getParams());

        Page<TlFacilitiesTypeItems> listPage = new Page<>(page, size, true);
        IPage<TlFacilitiesTypeItems> iPage = tlFacilitiesTypeItemsMapper.listTlFacilitiesTypeItems(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("factypeitemlist", iPage.getRecords());
        return json;
    }
    @Override
    public Map<String,TlFacilitiesTypeItems> getTlFacilitiesTypeItemsMap(QueryWrapper<TlFacilitiesTypeItems> facilitiesTypeItemsQueryWrapper){
        List<TlFacilitiesTypeItems> facilitiesTypeItemsList = tlFacilitiesTypeItemsMapper.selectList(facilitiesTypeItemsQueryWrapper);
        Map<String,TlFacilitiesTypeItems> facilitiesTypeItemsMap=new HashMap<>();
        if(CollectionUtils.isNotEmpty(facilitiesTypeItemsList)){
            facilitiesTypeItemsMap = GroupingByUtils.getGroupingForMapEntity(facilitiesTypeItemsList,TlFacilitiesTypeItems::getId,
                    Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return facilitiesTypeItemsMap;
    }
}
